﻿$(document).ready(function () {

    $("#refreshBtn").click(function () {
        //加载右边的内容
        var selector = "#subContent .rightContent";
        //根据当前选中的栏目获取要加载端url
        var url = $("#subNav span.selectedNav").attr("data-href");
        //取得筛选条件
        var treeObj = $.fn.zTree.getZTreeObj("tree");
        var nodeId = treeObj.getSelectedNodes()[0].id;
        url = url + "?id=" + nodeId;
        ajaxLoad(selector, url);
    });

    $("#createBtn").click(function () {
        var treeObj = $.fn.zTree.getZTreeObj("tree");
        var nodeId = treeObj.getSelectedNodes()[0].id;
        var nodeName = treeObj.getSelectedNodes()[0].name;
        var url = $(this).attr("data-href") + "?unitId=" + nodeId + "&unitName=" + nodeName;
        if (url != null && url != "") {
            artDialogForm(url, $(this).attr("data-text"), "operationmanagecontractcreate", 550, 315);
        }
    });

    $("#deleteBtn").click(function () {
        var row = $("table.tablelist tbody tr.tableselectedrow");
        if (row.length == 0) {
            artDialogAlert("请先选择一行");
            return false;
        }
        if (row.length > 1) {
            artDialogAlert("一次只能删除一行");
            return false;
        }
        artDialogConfirm("确定要删除这个合同吗？",
            function () {
                //commonajax.js中的方法
                myajax("/Contract/Delete",
                    row.attr("id"),
                    function (data) {
                        if (data.success == false) {
                            artDialogAlert(data.message);
                            return;
                        }

                        //删除当前行
                        row.remove();
                        //重新编号
                        //$("table.tablelist tbody tr").each(function (index) {
                        //    $(this).children().eq(0).html(index + 1);
                        //});
                    },
                    function (data) {
                        //artDialogAlert("删除失败");
                        artDialogAlert(data.statusText);
                    },
                    ".rightContent",
                    true
                    );
            }
            );
    });

    $("#editBtn").click(function () {
        var row = $("table.tablelist tbody tr.tableselectedrow");
        if (row.length == 0) {
            artDialogAlert("请先选择一行");
            return false;
        }
        if (row.length > 1) {
            artDialogAlert("一次只能编辑一行");
            return false;
        }
        var url = $(this).attr("data-href");
        url += ("/" + row.attr("id"));
        if (url != null && url != "") {
            artDialogForm(url, $(this).attr("data-text"), "operationmanagecontractcreate", 550, 315);
        }
    });

    $("#searchBtn").click(function () {
        $("#errorPic").hide();

        if ($("#pageIndex").length > 0) {
            $("#pageIndex").val("1");
        }
        $("#searchForm").submit();
    });

    $("#searchForm").submit(function () {
        $("#tablelist").children(":visible").remove();

        //取得筛选条件
        var treeObj = $.fn.zTree.getZTreeObj("tree");
        var nodeId = treeObj.getSelectedNodes()[0].id;
        $("#unitId").val(nodeId);
        return true;
    });

    registertableevent();

    $("#pagerDiv a").click(function () {
        if ($(this).attr("disabled") == "disabled") {
            return false;
        }
        //给searchform添加pageIndex参数
        $("<input type='hidden' name='pageIndex' id='pageIndex' value='" + $(this).attr("value") + "' />").insertAfter("#searchForm span:last");

        $("#searchForm").submit();
    });

    $("#pagerDiv select").change(function () {
        $("<input type='hidden' name='pageIndex' id='pageIndex' value='" + $(this).val() + "' />").insertAfter("#searchForm span:last");

        $("#searchForm").submit();
    });
});